package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/menu")
public class MenuController  {


    @Reference
    MenuService menuService;


    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,"新增菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增菜单失败");
        }

    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> menus = menuService.findAll();
            return new Result(true,"查询菜单成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }
    }

    @RequestMapping("/findByRoleId")
    public Result findByRoleId(Integer id){
        try {
            List<Integer> menusIds = menuService.findByRoleId(id);
            return new Result(true,"查询菜单成功",menusIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = menuService.findPage(queryPageBean);
        return page;
    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
        try {
            menuService.delById(id);
            return new Result(true,"删除菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除菜单失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"查询菜单成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单成功");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,"修改菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改菜单失败");
        }
    }

    @RequestMapping("/findFu")
    public Result findFu(){
        try {
            List<Menu> menuList =menuService.findFu();
            return new Result(true,"查询父菜单成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询父菜单失败");
        }
    }
    @RequestMapping("/findByUserName")
    public Result findByUserName(String username){
        try {
            return new Result(true,"查询菜单成功",menuService.findMenuByUserId(username));

        } catch (NullPointerException e){
            return new Result(false,"查询菜单失败 空指针异常",menuService.findMenuByUserId(username));
        }catch (ClassCastException e){
            return new Result(false,"查询菜单失败 类型转换异常");
        }
    }

}
